package org.example;

import io.netty.channel.ChannelHandlerContext;

import java.util.concurrent.ConcurrentHashMap;

/**
 * Author:qqw
 * Date:2024-06-18
 * Description:持有所有的 ForwarderServerHandler
 */
public class ForwarderServerHandlerHolder {

    private static ConcurrentHashMap<String,ForwarderServerHandler> handlerMap = new ConcurrentHashMap<>();

    public static ForwarderServerHandler getServerHandler(String service, ChannelHandlerContext receiveCtx, Object msg){
        ForwarderServerHandler handler;
        if((handler = handlerMap.get(service)) != null){
            return handler;
        }
        ForwarderServerHandler forwarderServerHandler = initHandler(receiveCtx, msg);
        handlerMap.put(service,forwarderServerHandler);
        return forwarderServerHandler;
    }

    private static ForwarderServerHandler initHandler(ChannelHandlerContext receiveCtx, Object msg){
        return new ForwarderServerHandler(receiveCtx,msg);
    }
}
